<?
class ListMenuNewsForm extends Form
{
	function ListMenuNewsForm()
	{
		Form::Form('ListMenuNewsForm');
		
        $this->link_css("style/paging.css");
	}
	
	function on_submit()
	{
            
	}
	
	function draw()
	{
		global $display;
        //$display->add("WEB_DIR",WEB_DIR);
         $id = Url::get("id");
            $cmd = Url::get('cmd');
            $mess = '';
            if($cmd =='del' && $id != ''){
                $menunews ["user_del"] = User::id();
                $menunews ["user_del_email"] = User::user_email();
                $menunews ["time_del"] = TIME_NOW;
                $menunews ["status"] = '-1';
                $check_del = DB::select_all('categories','id='.$id);
                foreach ($check_del as $c){
                        //Xóa 2 lần là xóa luôn khỏi DB
                    if($c['status']==-1){
                        //del
                        DB::delete('categories', 'id='.$id);
                        DB::delete('news', 'cate_id='.$id);
                    }else{
                        DB::update('categories',$menunews,'id='.$id);
                        //update các tin thuộc thư mục đó
                        $news ["user_del"] = User::id();
                        $news ["user_del_email"] = User::user_email();
                        $news ["time_del"] = TIME_NOW;
                        $news ["status"] = '-1';
                                DB::update('news',$news,'cate_id='.$id);
                    }
                }
                $mess = 'Đã xóa thành công !';
            }elseif ($cmd =='set_default') {
                $temp = array();
                $temp['set_default'] = '0';
                DB::update('categories',$temp,'set_default=1');
                $temp['set_default'] = '1';
                DB::update('categories',$temp,'id='.$id);
                
        }   
	$this->beginForm(false,"post",false,Url::build_current());
        $all_menu =  DB::select_all("categories");
        foreach ($all_menu as $key){
            $temp = DB::count('news', 'cate_search LIKE "%c'.$key['id'].'a%"');
            $all_menu[''.$key['id'].'']['count']= $temp;
        }
        $total_menu = count($all_menu);
               
                $display->add('all_menu',$all_menu);
                $display->add('total',$total_menu);
                $display->add('mess',$mess);
                if(User::have_permit('news_admin')){
                $news_admin = array(
                                        'tittle' => 'Quản lý tin tức',
                                        'url'   => WEB_DIR.ECRewrite::formatUrl('?page=news')
                );
                $display->add('news_admin',$news_admin);
                 }
		$display->output('MenuNewsList');
		$this->endForm();
	}
}
?>